//
//  AppDelegate.swift
//  Weibo_SwiftDemo
//
//  Created by gjh on 2020/12/12.
//

import UIKit
import SVProgressHUD
import AFNetworking

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // 设置全局额外方法
        setupAdditions()
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = JHNavigationController(rootViewController: JHMainTabBarController())
        window?.makeKeyAndVisible()
        loadAppInfo()
        return true
    }
}
// MARK: - 设置应用程序额外信息
extension AppDelegate {
    private func setupAdditions() {
        // 1.设置 SVProgressHUD 的最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1.0)
        // 2.设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        // 3. 设置用户授权显示通知
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            let notifySetting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySetting)
        }
    }
}

extension AppDelegate {
    
    private func loadAppInfo() {
        // 1\模拟异步
        DispatchQueue.global().async {
            // 1\ url
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            // 2\ data
            let data = NSData(contentsOf: url!)
            
            // 3\写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let json = (docDir as NSString).appendingPathComponent("/main.json")
            // 直接保存在沙盒,等待下一次程序启动使用
            data?.write(toFile: json, atomically: true)
            
            print("应用程序加载完毕\(json)")
            
            
        }
    }
}
